package com.zzy.java_gobang.game;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzy.java_gobang.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

@Component
public class Matcher {
    private Queue<User> normalQueue = new LinkedList<User>();
    private Queue<User> highQueue = new LinkedList<User>();
    private Queue<User> veryHightQueue = new LinkedList<User>();

    @Autowired
    private OnlineUserManager onlineUserManager;

    @Autowired
    private RoomManager roomManager;


    private ObjectMapper objectMapper;

    public void add(User user) {
        if(user.getScore()<2000){
            synchronized (normalQueue) {
                normalQueue.offer(user);
                normalQueue.notify();
            }
            //normalQueue.add(user);
            System.out.println("把玩家"+user.getUsername()+"放到了normalQueue队列里面");
        }else if(user.getScore()>2000&&user.getScore()<3000){
            synchronized (highQueue) {
                highQueue.offer(user);
                highQueue.notify();
            }
            //highQueue.add(user);
            System.out.println("把玩家"+user.getUsername()+"放到了highQueue队列里面");
        }else{
            synchronized (veryHightQueue) {
                veryHightQueue.offer(user);
                veryHightQueue.notify();
            }
            //veryHightQueue.add(user);
            System.out.println("把玩家"+user.getUsername()+"放到了veryHighQueue队列里面");
        }
    }

    public void remove(User user) {
        if(user.getScore()<2000){
            synchronized (normalQueue) {
                normalQueue.remove(user);
            }
            //normalQueue.remove(user);
        }else if(user.getScore()>2000&&user.getScore()<3000){
            synchronized (highQueue) {
                highQueue.remove(user);
            }
            //highQueue.remove(user);
        }else{
            synchronized (veryHightQueue) {
                veryHightQueue.remove(user);
            }
            //veryHightQueue.remove(user);
        }
    }


    public Matcher(){
        Thread t1=new Thread(){
            @Override
            public void run() {
                while(true){
                    handleMatcher(normalQueue);
                }
            }
        };
        t1.start();

        Thread t2=new Thread(){
            @Override
            public void run() {
                while(true){
                    handleMatcher(highQueue);
                }
            }
        };
        t2.start();


        Thread t3=new Thread(){
            @Override
            public void run() {
                while(true){
                    handleMatcher(veryHightQueue);
                }
            }
        };
        t3.start();
    }

    public void handleMatcher(Queue<User> matchQueue){
        synchronized(matchQueue){
            try{
                while(matchQueue.size()<2){
                    matchQueue.wait();
                }
                    User player1=matchQueue.poll();
                    User player2=matchQueue.poll();
                    System.out.println("匹配出来两个玩家"+player1.getUsername()+","+player2.getUsername());

                    WebSocketSession session1 = onlineUserManager.getFromGameHall(player1.getUserId());
                    WebSocketSession session2 = onlineUserManager.getFromGameHall(player2.getUserId());

                    if(session1==null){
                        matchQueue.offer(player2);
                        return;
                    }

                    if(session2==null){
                        matchQueue.offer(player1);
                        return;
                    }

                    if(session1==session2){
                        matchQueue.offer(player1);
                    }

                    //  TODO 把两个玩家放到一个房间里面
                Room room = new Room();
                roomManager.add(room, player1.getUserId(), player2.getUserId());


                //下面的这个就是对于我们的玩家一的响应
                    MatchResponce response1=new MatchResponce();
                    response1.setOk(true);
                    response1.setMessage("matchSuccess");
                    String json1 = objectMapper.writeValueAsString(response1);
                    session1.sendMessage(new TextMessage(json1));


                    //下面的这个就是对于玩家二的响应
                    MatchResponce response2=new MatchResponce();
                    response2.setOk(true);
                    response2.setMessage("matchSuccess");
                    String json2 = objectMapper.writeValueAsString(response2);
                    session2.sendMessage(new TextMessage(json2));

                }
                catch(IOException | InterruptedException e){
                    e.printStackTrace();

                }

        }



    }
}
